<?php


/**
 * 商品管理
 * Enter description here ...
 * @author Admin
 *
 */
class GoodsAction extends CommonAction{
	
	
	/**
	 * 商品列表
	 * Enter description here ...
	 */
	public function index() {
		
		$goods = D('Goods');
		$prefix = C('DB_PREFIX');   //配置参数中获取数据表前缀
			
		$goodsname =  $_REQUEST['goodsname'];      //检索条件
		$this->assign("goodsname",$goodsname);	
		
		$goodsmap['goodsname']= array('like',"%{$goodsname}%"); 
			
			
		import("@.ORG.Page"); //导入分页类
		
		$count = $goods->where($goodsmap)->count();    //计算总数
		
		$p = new Page ($count,12);
		
		
		$goodsdata = $goods->Table("{$prefix}goods as G")->
							join("{$prefix}category as C ON G.cid = C.id")->
							field('G.*,C.categoryname')->
							where($goodsmap)->
							limit($p->firstRow.','.$p->listRows)->   //分页查询
							order('id desc')->findAll();
		
					
		//...分页检索条件	
		$p->parameter = "goodsname=" . urlencode ( $_REQUEST['goodsname'] ) . "&";					
							
							
		$page = $p->show();
		$this->assign("page",$page);
		$this->assign("goodsdata",$goodsdata);
		
		$this->display();
	}
	
	
	
	
	/**
	 * 商品添加
	 * @see CommonAction::add()
	 */
	public function add(){
		$Category = D('Category');
		//层级结构的商品分类
		$list=$Category->field("id,categoryname,fid,path,concat(path,'-',id) as bpath")->where('state=1')->order('bpath')->select();   //获取启用状态的类型
		
		foreach($list as $key=>$value){
			$list[$key]['count']=count(explode('-',$value['bpath']));
		}
		
		$this->assign("categorydata",$list);
		
		parent::add();  
  	}
	
	
  	
  	
  	
	/**
	 * 商品添加保存
	 * @see CommonAction::insert()
	 */
	public function insert(){
		$data = Init_GP(array('goodsname','cid','goodsfreight','goodscode','amount'));   //商品名称、类别、运费 、编号
		$data['goodsbrief']=HtmlReplace($_POST['goodsbrief']);  //大文本内容 简介和描述
		$data['goodsabout']=HtmlReplace($_POST['goodsabout']);
		
		$goods = D('Goods');
		$data = $goods->create($data);
		
		if(empty($data) || $data === false) {
			$this->error($goods->getError());
		}
		$id = $goods->add($data);    //保存商品
		
		if($id) { //保存成功...处理商品图片
			$v = Init_GP(array('imgs'));   //商品图片
			$imgs = $v['imgs'];
			
			if (!empty($imgs)){
				$accessory_relation = M('Accessory_relation');  //附件业务功能关联表
				$accessory_relationdata['relationid'] =$id;
				$accessory_relationdata['table'] = "Goods";
				
				foreach ($imgs as $val){
					$accessory_relationdata['accessoryid'] = $val;
					$value = $accessory_relation->create($accessory_relationdata);
					$accessory_relation->add($value);
					unset($accessory_relationdata['accessoryid']);
				}
			}
			
			$this->assign('jumpUrl','__URL__/index');
			$this->success("添加成功");
		}else {
			//失败提示
			$this->error($goods->getError());
		}
	}
	
	
	
	
	
	/**
	 * 修改商品
	 * @see CommonAction::edit()
	 */
	public function edit(){
		$goods	= D('Goods');
		$id     = GetNum($_GET['id']); 
		$vo	=	$goods->find($id);     //根据主键获取商品
		
		$Category = D('Category');
		//层级结构的商品分类
		$list=$Category->field("id,categoryname,fid,path,concat(path,'-',id) as bpath")->where('state=1')->order('bpath')->select();
		
		foreach($list as $key=>$value){
			$list[$key]['count']=count(explode('-',$value['bpath']));
		}
		
		
		$accessory_relation = M('Accessory_relation');     //获取商品关联附件图片数据
		$prefix = C('DB_PREFIX');
		$accessorydata = $accessory_relation->Table("{$prefix}accessory_relation as A")->
					  join("{$prefix}accessory as B ON A.accessoryid = B.id")->
					  field('B.*')->
					  where("A.relationid = {$id} and A.table = 'Goods'")->
					  findAll();
					  
		
		$this->assign("categorydata",$list);
		
		$this->assign('data',$vo);
		$this->assign('accessorydata',$accessorydata);
		
		$this->display();
	}
	
	
	
	
	
	/**
	 * 商品修改保存
	 * @see CommonAction::update()
	 */
	public function update(){
		$id = GetNum($_POST['id']);  //主键
		$awhere['id'] = $id;
		$where['relationid'] = array('eq',$id);
		$where['table'] = array('eq','Goods');
		
		$data = Init_GP(array('goodsname','cid','goodsfreight','goodscode','amount'));   //商品名称、类别、运费 、编号
		$data['goodsbrief']=HtmlReplace($_POST['goodsbrief']);  //大文本数据
		$data['goodsabout']=HtmlReplace($_POST['goodsabout']);
		
		//删除旧关系....图片商品关联
		$accessory_relation = M('Accessory_relation');
		$accessory_relation->where($where)->delete();
		
		//是否有图片
		$v = Init_GP(array('imgs'));
		$imgs = $v['imgs'];
		//添加新关系
		if (!empty($imgs)){
				$accessory_relationdata['relationid'] =$id;
				$accessory_relationdata['table'] = "Goods";
				
				foreach ($imgs as $val){
					if(!empty($val)){
						$accessory_relationdata['accessoryid'] = $val;
						$value = $accessory_relation->create($accessory_relationdata);
						$info = $accessory_relation->add($value);
						unset($accessory_relationdata['accessoryid']);
					}
				}
		}
		
		$goods = D('Goods');
		
		$data = $goods->create($data,2);
		if(empty($data) || $data === false) {
			$this->error($goods->getError());
		}
		
		$id = $goods->where($awhere)->save($data);
		
		if($id) { //保存成功
			$this->assign('jumpUrl','__URL__/index');
			$this->success("编辑成功");
		}else {
			$error = $goods->getError();
			if (empty($error)) {
				$this->assign('jumpUrl','__URL__/index');
				$this->success("编辑成功");
			}else {
				$this->error($error);
			}
		}
	}
	
	
	
	
	
	
	/**
	 * 删除商品（单个）
	 * @see CommonAction::del()
	 */
	public function del(){
		$id = GetNum($_GET['id']);
		
		$where['relationid'] = array('eq',$id);
		$where['table'] = array('eq','Goods');
		
		$accessory_relation = M('Accessory_relation');  //删除商品关联图片附件
		$accessory_relation->where($where)->delete();
		
		parent::del($id);  //删除商品
	}
	
	
	/**
	 *  删除商品（多个）
	 * @see CommonAction::dels()
	 */
	public function dels(){
		$data = Init_GP(array('ids'));
		$ids = $data['ids'];
		$tmp       = getdotstring($ids,'int');
		$where['relationid'] = array('in',$tmp);
		$where['table'] = array('eq','Goods');
		$accessory_relation = M('Accessory_relation');
		$accessory_relation->where($where)->delete();   //删除商品关联图片附件
		
		parent::dels($ids);    //删除商品
	}
	
	
	
	
	
	
}
?>